import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ViewQueryFrame extends JFrame {
    private JComboBox<String> viewComboBox; // 下拉菜单，用于选择视图
    private JButton queryButton; // 查询按钮
    private JTable resultTable; // 用于显示查询结果的表格
    private DefaultTableModel tableModel; // 表格模型

    public ViewQueryFrame() {
        setTitle("视图查询");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化组件
        initComponents();
    }

    private void initComponents() {
        // 创建下拉菜单，列出所有可用的视图
        String[] views = {"库存状态视图", "供应商采购明细视图", "销售员销售业绩视图", "客户销售明细视图",
                "货物存储位置视图", "报损明细视图", "采购员采购业绩视图", "仓位库存明细视图"};
        viewComboBox = new JComboBox<>(views);

        // 查询按钮
        queryButton = new JButton("查询");

        // 表格模型
        tableModel = new DefaultTableModel();
        resultTable = new JTable(tableModel);

        // 添加组件到面板
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(new JLabel("选择视图："));
        topPanel.add(viewComboBox);
        topPanel.add(queryButton);

        // 添加表格到滚动面板
        JScrollPane scrollPane = new JScrollPane(resultTable);

        // 添加面板到窗体
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);

        // 添加事件监听
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryView();
            }
        });
    }

    private void queryView() {
        String selectedView = (String) viewComboBox.getSelectedItem();
        String query = getQuery(selectedView);

        // 清空表格模型
        tableModel.setRowCount(0);
        tableModel.setColumnCount(0);

        try {
            // 使用DBUtil获取连接
            Connection conn = DBUtil.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            // 获取列名
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                tableModel.addColumn(metaData.getColumnName(i));
            }

            // 获取数据
            while (rs.next()) {
                Object[] row = new Object[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    row[i - 1] = rs.getObject(i);
                }
                tableModel.addRow(row);
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "查询失败：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String getQuery(String viewName) {
        switch (viewName) {
            case "库存状态视图":
                return "SELECT * FROM InventoryStatus";
            case "供应商采购明细视图":
                return "SELECT * FROM SupplierPurchaseDetails";
            case "销售员销售业绩视图":
                return "SELECT * FROM SalesmanPerformance";
            case "客户销售明细视图":
                return "SELECT * FROM CustomerSalesDetails";
            case "货物存储位置视图":
                return "SELECT * FROM GoodsStorageLocation";
            case "报损明细视图":
                return "SELECT * FROM DamageDetails";
            case "采购员采购业绩视图":
                return "SELECT * FROM BuyerPerformance";
            case "仓位库存明细视图":
                return "SELECT * FROM WarehouseInventoryDetails";
            default:
                return "";
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ViewQueryFrame().setVisible(true);
            }
        });
    }
}